package com.dayouzi.crawler_monitor.config;

import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Cors跨域请求访问配置类
 */
@Configuration
public class CorsFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse httpResponse = (HttpServletResponse)servletResponse;

        //允许跨域访问的Origin域名
        httpResponse.setHeader("Access-Control-Allow-Origin","http://localhost:9528");
        httpResponse.setHeader("Access-Control-Allow-Origin","http://localhost:8080");
        httpResponse.setHeader("Access-Control-Allow-Origin","*");
        //是否允许发送Cookie
        httpResponse.setHeader("Access-Control-Allow-Credentials","true");
        //允许浏览器访问的Header
        httpResponse.setHeader("Access-Control-Expose-Headers", "token");
        //允许访问的请求方式
        httpResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");

        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }

}
